#!/usr/python
# -*- coding: utf-8 -*-


# cmap2owl -- Helper application to convert from concept maps to OWL ontologies
# Copyright (c) 2008-2013  Rodrigo Rizzi Starr
#  
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#  
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#  
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.


'''
This file contains the Portuguese-specific Natural Language Processing part of
the application.


@author: Rodrigo Rizzi Starr
@copyright: Copyright © 2008-2013 Rodrigo Rizzi Starr
@license: MIT License
@contact: rodrigo.starr@gmail.com
'''


import os
import re
import sys
import yaml
import nltk
import collections
import cPickle
from nltk.corpus import mac_morpho
from nltk.tag import SequentialBackoffTagger


class PortugueseRegexpTagger(SequentialBackoffTagger):
    '''
    This is a simple reimplementation of NLTK regex tagger hard-wired for
    Portuguese. This was done because some internal changes in the NTLK tagger
    turned pickling invalid between 2.0 & 2.04 and the application has to
    support both versions.
    '''

    def __init__(self, backoff=None):

        self._regexps = [
            (r'.*nte$', 'ADV'),
            (r'.*(a|i)d(o|a)(s)?$', 'PCP'),
            (r'.*ic(o|a)(s)?$', 'ADJ'),
            (r'.*ntes$', 'ADJ'),
            (r'.*ve(l|is)$', 'ADJ'),
            (r'ndo|ram|ava|rem|ria|amos|iam|vam|sse|tou|ará|tam|emos|rou', 'V'),
            (r'^-?[0-9]+((.|,)[0-9]+)*$', 'NUM'),
            (r'^&$', 'KC'),
            (r'^\.$', '.'),
            (r'^;$', ';'),
            (r'^:$', ':'),
            (r'.*ção$', 'N'),
            (r'.*ções$', 'N'),
            ]
        
        SequentialBackoffTagger.__init__(self, backoff)
        self._size = len(self._regexps)

    def choose_tag(self, tokens, index, history):
        for regexp, tag in self._regexps:
            if re.match(regexp, tokens[index]): # ignore history
                return tag         
        return None

    def __repr__(self):
        return '<Portuguese Regexp Tagger: size=%d>' % self._size


